function matrixToGraph(obj) {
  const { matrix, labels } = obj;
  const len = matrix.length;
  let communities = [];
  if (labels[0].length) {
    communities = labels;
  } else {
    const tempArr = labels.slice(0, len);
    let k = -1;
    for (let i = 0; i < len; i++) {
      if (tempArr[i] === -1) continue;
      communities.push([]);
      k++;
      const temp = tempArr[i];
      for (let j = i; j < len; j++) {
        if (tempArr[j] !== -1 && tempArr[j] === temp) {
          communities[k].push(j);
          tempArr[j] = -1;
        }
      }
    }
  }
  if (matrix.length !== matrix[0].length) {
    throw new Error('请输入邻接矩阵');
  }
  const nodeLen = matrix.length;
  let nodes = [];
  for (let i = 0; i < nodeLen; i++) {
    let node = {};
    node.id = '' + i;
    node.cluster = '' + communities.findIndex(community => community.includes(i));
    nodes.push(node);
  }
  let edges = [];
  for (let i = 0; i < nodeLen; i++) {
    for (let j = i; j < nodeLen; j++) {
      if (matrix[i][j] !== 0) {
        let edge = {};
        edge.source = nodes[i].id;
        edge.target = nodes[j].id;
        if (matrix[i][j] === 1) {
          edge.signed = '+';
        } else {
          edge.signed = '-';
        }
        if (
          (nodes[i].cluster === nodes[j].cluster && matrix[i][j] === -1) ||
          (nodes[i].cluster !== nodes[j].cluster && matrix[i][j] === 1)
        ) {
          edge.isBalanceEdge = false;
        } else {
          edge.isBalanceEdge = true;
        }
        edges.push(edge);
      }
    }
  }
  return {
    nodes,
    edges,
    communities,
  };
}
export default matrixToGraph;
